﻿using UnityEngine;
using System.Collections;

namespace GGJam14
{
	public enum TalkBubbleState
	{
		HIDDEN, OPENNING, CLOSING, SHOWING
	}

	public class TalkBubble : MonoBehaviour
	{
		#region Variables

		public TalkBubbleState state = TalkBubbleState.HIDDEN;

		public static float showTime = 3f;
		private float hideSchedule = 0f;

		public float closedSize = 0.1f;
		public float openSize = 1.8f;
		public float transitionSpeed = 0.5f;

		#endregion
		#region Engine Calls

		void Awake ()
		{
			SwitchToHidden();
		}

		void Update()
		{
			//FSM
			if (state == TalkBubbleState.CLOSING) UpdateClosing();
			else if (state == TalkBubbleState.OPENNING) UpdateOpenning();
			else if (state == TalkBubbleState.HIDDEN) UpdateHidden();
			else if (state == TalkBubbleState.SHOWING) UpdateShowing();
		}

		bool CheckForHide()
		{
			if (Time.time > hideSchedule)
			{
				HideBubble();
				return true;
			}

			return false;
		}

		#endregion
		#region States

		void UpdateOpenning()
		{
			transform.localScale = new Vector3(
				transform.localScale.x + transitionSpeed * Time.deltaTime,
				transform.localScale.y,
				transform.localScale.z
				);
			
			if (transform.localScale.x >= openSize)
			{
				SwitchToShowing();
			}
		}

		void UpdateClosing()
		{
			transform.localScale = new Vector3(
				transform.localScale.x - transitionSpeed * Time.deltaTime,
				transform.localScale.y,
				transform.localScale.z
				);

			if (transform.localScale.x <= closedSize)
			{
				SwitchToHidden();
			}
		}

		void UpdateShowing()
		{
			//verifica se a bubble deve sair
			if (state != TalkBubbleState.HIDDEN && Time.time > hideSchedule)
			{
				SwitchToClosing();
			}
		}

		void UpdateHidden()
		{
			//acho que não faz nada mesmo
		}

		void SwitchToHidden()
		{
			state = TalkBubbleState.HIDDEN;
			renderer.enabled = false;

			transform.localScale = new Vector3(
				closedSize,
				transform.localScale.y,
				transform.localScale.z
				);
		}

		void SwitchToClosing()
		{
			state = TalkBubbleState.CLOSING;
		}

		void SwitchToOpenning()
		{
			state = TalkBubbleState.OPENNING;
			renderer.enabled = true;
		}

		void SwitchToShowing()
		{
			state = TalkBubbleState.SHOWING;

			transform.localScale = new Vector3(
				openSize,
				transform.localScale.y,
				transform.localScale.z
				);

			ScheduleHide();
		}

		#endregion
		#region Methods

		[ContextMenu("Show Talk Bubble")]
		void CallShowTalkBubble()
		{
			ShowTalkBubble("This is a Talk Bubble!");
		}

		void ShowTalkBubble(string text)
		{
			Debug.Log("Show Talk Bubble: " + text, this);

			SwitchToHidden();

			//switch TODO colocar o valor certo
			renderer.sharedMaterial.mainTextureOffset = new Vector2(
				renderer.sharedMaterial.mainTextureOffset.x,
				renderer.sharedMaterial.mainTextureOffset.y
				);

			//show
			SwitchToOpenning();
		}

		[ContextMenu("Show Thought Bubble")]
		void CallShowThoughtBubble()
		{
			ShowTalkBubble("This is a Thought Bubble!");
		}

		void ShowThoughtBubble(string text)
		{
			Debug.Log("Show Thought Bubble: " + text, this);

			SwitchToHidden();

			//switch TODO colocar o valor certo
			renderer.material.mainTextureOffset = new Vector2(
				renderer.material.mainTextureOffset.x,
				renderer.material.mainTextureOffset.y
				);

			//show
			SwitchToOpenning();
		}

		void ScheduleHide()
		{
			hideSchedule = Time.time + TalkBubble.showTime;
		}

		[ContextMenu("Hide Bubble")]
		public void HideBubble()
		{
			SwitchToHidden();
		}

		#endregion
	}
}
